<template>
  <!-- home -->
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header style="z-index: 2">
      <div>
        <img src="../assets/logo.png" alt="">
        <span>电商管理后台系统</span>
      </div>
      <span style="color: white" class="name">
        欢迎您： {{ name }}
      </span>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" style="z-index: 2">
        <!-- 导航区域 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="active"
        >
          <!-- 一级导航 -->
          <el-submenu
            v-for="(item) in menuList"
            :key="item.id"
            :index="item.id + ''"
          >
            <template slot="title">
              <!-- 图标 -->
              <i class="oneIcon" :class="iconsObj[item.id]"></i>
              <!-- 导航名称 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级导航 -->
            <!-- 给 menu 添加路由 点击通过 index 跳转 -->
            <el-menu-item
              :index="'/' + child.path"
              v-for="child in item.children"
              :key="child.id"
              @click="saveActive('/' + child.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级导航名 -->
                <span>{{ child.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <!-- 占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 列表数据
      menuList: [],
      iconsObj: {
        125: 'el-icon-s-custom',
        0: 'el-icon-s-custom',
        103: 'el-icon-s-help',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-data',
        145: 'el-icon-s-grid'
      },
      // 是否折叠
      isCollapse: false,
      // 激活菜单
      active: '',
      name: ''
    }
  },
  created () {
    this.getmenuList()
    this.active = sessionStorage.getItem('active')
    this.$nextTick(() => {
      console.log(this.menuList)
    })
    this.name = sessionStorage.getItem('name')
  },
  methods: {
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getmenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.menuList = res.data
      const obj = {
        authName: 'vue工具',
        children: [
          {
            authName: '拖拽',
            children: [
              {}
            ],
            order: null,
            path: 'drag'
          },
          {
            authName: '图表',
            children: [
              {}
            ],
            order: null,
            path: 'echarts'
          },
          {
            authName: '缩放Div',
            children: [
              {}
            ],
            order: null,
            path: 'zoom'
          },
          {
            authName: '拖拽布局',
            children: [
              {}
            ],
            order: null,
            path: 'gridLayout'
          },
          {
            authName: '全屏',
            children: [
              {}
            ],
            order: null,
            path: 'full'
          },
          {
            authName: '卡片翻转',
            children: [
              {}
            ],
            order: null,
            path: 'rotateCard'
          },
          {
            authName: '支付宝',
            children: [
              {}
            ],
            order: null,
            path: 'zhifu'
          },
          {
            authName: '游戏',
            children: [
              {}
            ],
            order: null,
            path: 'game'
          },
          {
            authName: '事件总线',
            children: [
              {}
            ],
            order: null,
            path: 'events'
          }
        ],
        order: 99,
        id: 0,
        path: 'all'
      }
      this.menuList.push(obj)
      console.log(this.menuList)
    },
    // 切换折叠
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 高亮
    saveActive (path) {
      sessionStorage.setItem('active', path)
      this.active = sessionStorage.getItem('active')
    }
  }
}
</script>

<style lang="less" scoped>
.name {
  position: absolute;
  right: 120px;
}
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  div {
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 20px;
    span {
      margin-left: 15px;
    }
  }
  img {
    width: 50px;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}
.el-footer {
  background-color: #eaedf1;
}
.oneIcon {
  margin-right: 13px;
  font-size: 18px;
}
.toggle-button {
  background: #4A5064;
  text-align: center;
  color: white;
  letter-spacing: 0.2em;
  font-size: 10px;
  line-height: 24px;
  cursor: pointer;
}
</style>
